package com.app.zenmewan.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;


@RestController
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(FileController.class);
    @GetMapping("/hello")
    public void home(){
        log.info("收到");
    }
    class UploadImgObj{
        public UploadImgObj(String url) {
            this.url = url;
        }

        String uploaded = "1";
        String url = "http://localhost:8010/picReq";

        public String getUploaded() {
            return uploaded;
        }

        public void setUploaded(String uploaded) {
            this.uploaded = uploaded;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
    @GetMapping("/getImage")
    public void pic(@RequestParam("name")String name, HttpServletRequest request, HttpServletResponse response) throws IOException{
        //文件名
        //这里是存放图片的文件夹地址
        String path= "E:/files/"+name;
        FileInputStream fileIs=null;
        OutputStream outStream = null;
        try {
            fileIs = new FileInputStream(path);
            //得到文件大小
            int i=fileIs.available();
            //准备一个字节数组存放二进制图片
            byte data[]=new byte[i];
            //读字节数组的数据
            fileIs.read(data);
            //设置返回的文件类型
            response.setContentType("image/*");
            //得到向客户端输出二进制数据的对象
            outStream=response.getOutputStream();
            //输出数据
            outStream.write(data);
            outStream.flush();
        } catch (Exception e) {
            log.info("系统找不到图像文件："+path+"/"+name);

        }finally {
            try{
                //关闭输出流
                outStream.close();
                //关闭输入流
                fileIs.close();
            }catch (NullPointerException e){
//                System.out.println(e);
            }
        }

    }

    @PostMapping("/eleupload")
    public UploadImgObj eleupload(@RequestPart(value = "file") MultipartFile file){

        try {
            if (file.isEmpty()) {
                return null;
//                return new UploadImgObj();
            }
            String fileName = file.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String uid = UUID.randomUUID().toString();
            fileName = uid+suffixName;
            log.info("上传的文件名为：" + fileName+" 后缀名为" + suffixName);
            // 设置文件存储路径(G盘),你可以存放在你想要指定的路径里面。
            String filePath = "E:\\files\\";
            String path = filePath + fileName;
            File dest = new File(path);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();// 新建文件夹
            }
            file.transferTo(dest);// 文件写入
            return new UploadImgObj("http://localhost:8010/getImage?name="+fileName);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
//        return new UploadImgObj();
    }
    @PostMapping("/upload")
    public UploadImgObj hello(@RequestPart(value = "upload") MultipartFile file){

        try {
            if (file.isEmpty()) {
                return null;
//                return new UploadImgObj();
            }
            String fileName = file.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String uid = UUID.randomUUID().toString();
            fileName = uid+suffixName;
            log.info("上传的文件名为：" + fileName+" 后缀名为" + suffixName);
            // 设置文件存储路径(G盘),你可以存放在你想要指定的路径里面。
            String filePath = "E:\\files\\";
            String path = filePath + fileName;
            File dest = new File(path);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();// 新建文件夹
            }
            file.transferTo(dest);// 文件写入
            return new UploadImgObj("http://localhost:8010/getImage?name="+fileName);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
//        return new UploadImgObj();
    }
    @GetMapping("/download")
    public String downloadFile(HttpServletRequest request,
                               HttpServletResponse response) throws IOException {
        String fileName = "";// 文件名
        if (fileName != null) {
            //设置文件路径
            File file = new File("E:\\files\\点赞.png");
            if (file.exists()) {
                response.setContentType("application/force-download");
                response.addHeader("Content-Disposition", "attachment;fileName="
                        + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "download success";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    bis.close();
                    fis.close();
                }
            }
        }
        return "failure";
    }

}
